package com.java46.front.base.action;

import com.java46.front.base.entity.GenericResult;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: BaseAction
 * @Description: 创建一个Action的父类用于action子类继承
 * @author: zuxia
 * @date: 2018年7月4日 下午2:28:33
 */
//配置注解
@ParentPackage(value="pkg")//包
@Namespace(value="/") //路径
@Results(value= {
		//出现异常之后统一处理异常的跳转地址，出现异常都统一抛出input处理异常
		@Result(name = "input", location = "/error.jsp"),
		//统一接收json数据
		@Result(name="json", type="json", params = {"root", "result"})
})
public abstract class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware  {

	/** @Fields serialVersionUID : 序列化ID  **/
	private static final long serialVersionUID = -8955728473438646250L;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	//得到session对象的setAttribute方法
	protected void setSessionAttribute(String key, Object value) {
		HttpSession session=	request.getSession();
		session.setAttribute(key, value);
	}
	////得到session对象的getAttribute方法
	protected Object getSessionAttribute(String key) {
		HttpSession session = request.getSession();
		return session.getAttribute(key);
	}

	//得到封装json数据的实体
	protected GenericResult result;
	public GenericResult getResult() {
		return result;
	}
	public void setResult(GenericResult result) {
		this.result = result;
	}
	public void setSuccesResult(Object obj) {
		result = new GenericResult();
		result.setSuccess(true);
		result.setData(obj);
	}

}
